package com.web.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.web.annotation.CurrentUser;
import com.web.domain.Response;
import com.web.entity.*;
import com.web.service.ExpertsService;
import com.web.service.HomeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/app/skill")
public class SkillController extends BaseController {
    @Autowired
    private ExpertsService expertsService;

    @Autowired
    private HomeService homeService;
    /**
     * 技能-技术
     * @param info
     * @param pageNum
     * @return
     */
    @RequestMapping("/hot")
    public Response getSkillHot(@CurrentUser UserInfo info, Integer pageNum){
        if (pageNum == null){
            return Response.custom().failure("页码不能为空!");
        }
        Map<String,Object> map = new HashMap<>();
        if (pageNum == 1){
            PageHelper.startPage(pageNum,10);
            List<HotExpertBean> list = expertsService.getHotExpert(info.getUserId());
            PageInfo pageInfo = new PageInfo(list,10);
            map.put("expert",pageInfo.getList());

            PageHelper.startPage(pageNum,10);
            List<HotUserData> list2 = expertsService.getHotUserVideos();
            PageInfo pageInfo2 = new PageInfo(list2,10);
            map.put("videos",pageInfo2.getList());

            PageHelper.startPage(pageNum,10);
            List<HotUserTextBean> list3 = expertsService.getHotUserText();
            PageInfo pageInfo3 = new PageInfo(list3,10);
            map.put("text",pageInfo3.getList());
        }else{
            map.put("expert",new List[0]);
            map.put("videos",new List[0]);
            PageHelper.startPage(pageNum,10);
            List<HotUserTextBean> list3 = expertsService.getHotUserText();
            PageInfo pageInfo3 = new PageInfo(list3,10);
            if (pageNum > pageInfo3.getPages()){
                map.put("text",new List[0]);
            }else{
                map.put("text",pageInfo3.getList());
            }
        }
        return Response.custom().ok().addParams(map);
    }

    /**
     * 技能-问答
     * @param info
     * @param pageNum
     * @return
     */
    @RequestMapping("/QuestionAndAnswer")
    public Response QuestionAndAnswer(@CurrentUser UserInfo info, Integer pageNum){
        if(pageNum == null){
            return Response.custom().failure("页码不能为空!");
        }
        Map<String,Object> map = new HashMap<>();
        if (pageNum == 1){
            PageHelper.startPage(pageNum,10);
            List<HotExpertBean> list = expertsService.getHotExpert(info.getUserId());
            PageInfo pageInfo = new PageInfo(list,10);
            map.put("expert",pageInfo.getList());

            PageHelper.startPage(pageNum,10);
            List<UserQuestion> list2 = expertsService.getUserQuestion();
            PageInfo pageInfo2 = new PageInfo(list2,10);
            map.put("UserQuestion",pageInfo2.getList());

            PageHelper.startPage(pageNum,10);
            List<UserAnswer> list3 = expertsService.getUserAnswer();
            PageInfo pageInfo3 = new PageInfo(list3,10);
            map.put("UserAnswer",pageInfo3.getList());
        }else{
            map.put("expert",new List[0]);
            map.put("UserQuestion",new List[0]);
            PageHelper.startPage(pageNum,10);
            List<UserAnswer> list3 = expertsService.getUserAnswer();
            PageInfo pageInfo3 = new PageInfo(list3,10);
            if (pageNum > pageInfo3.getPages()){
                map.put("UserAnswer",new List[0]);
            }else{
                map.put("UserAnswer",pageInfo3.getList());
            }
        }
        return Response.custom().ok().addParams(map);
    }

    /**
     * 问答详情
     * @param info
     * @param id
     * @return
     */
    @RequestMapping("/QuestionDetails")
    public Response QuestionDetails(@CurrentUser UserInfo info, Integer id){
        if(id == null){
            return Response.custom().failure("缺少请求参数,请重新核对后再提交!！");
        }
        Map<String,Object> map = new HashMap<>();
        map.put("userAnswer",expertsService.selectById("auq.id",String.valueOf(id)));
        PageHelper.startPage(1,10);
        PageInfo pageInfo = new PageInfo(homeService.getInstantLifeComment("3",String.valueOf(id)),10);
        if (pageInfo.getSize() == 0){
            map.put("comment",new List[0]);
        }else{
            if (pageInfo.getList().get(0) != null){
                map.put("comment",pageInfo.getList());
            }else{
                map.put("comment",new List[0]);
            }
        }
        return Response.custom().ok().addParams(map);
    }








}
